package com.example.videolib

import android.app.Activity
import android.graphics.SurfaceTexture
import android.view.TextureView
import androidx.navigation.Navigation
import com.chad.library.adapter.base.BaseViewHolder
import com.example.videolib.bean.RcvData
import com.hikvision.open.hikvideoplayer.HikVideoPlayer
import com.hikvision.open.hikvideoplayer.HikVideoPlayerCallback
import com.hikvision.open.hikvideoplayer.HikVideoPlayerFactory
import java.text.MessageFormat

/**
 * Author: sxygsj
 * Date: 2020/5/8 16:22
 * Description: item 播放
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class VideoPlay(val rcvData: RcvData) : HikVideoPlayerCallback, TextureView.SurfaceTextureListener{
    var mPlayer: HikVideoPlayer = HikVideoPlayerFactory.provideHikVideoPlayer()
    var mPlayerStatus = PlayerStatus.IDLE

    private lateinit var viewHolder: BaseViewHolder
    private var activity: Activity? = null
    fun play(holder: BaseViewHolder){
        val textureView = holder.getView<TextureView>(R.id.video_item_txv)
        this.activity = textureView.context as Activity
        holder.setText(R.id.video_name_txt, rcvData.cameraName)
        viewHolder = holder
        if (rcvData.url_rtsp== null) {
            viewHolder.setVisible(R.id.t_err, true)
            viewHolder.setText(R.id.t_err, "无信号")
            viewHolder.setGone(R.id.item_progress, false)
            return
        }
        textureView.surfaceTextureListener = this
    }
    private fun startPlay(){
        Thread(Runnable {
            mPlayerStatus = PlayerStatus.LOADING
            activity?.runOnUiThread(Runnable { viewHolder.setGone(R.id.item_progress, true) })

            if (rcvData.url_rtsp != null) {
                if (!mPlayer.startRealPlay(rcvData.url_rtsp, this)) {
                    onPlayerStatus(HikVideoPlayerCallback.Status.FAILED, mPlayer.lastError)
                }
            }
        }).start()
    }

    /**
     * @Description: [播放状态的回调]
     * @Param:
     * @Return:
     */
    override fun onPlayerStatus(status: HikVideoPlayerCallback.Status, i: Int) {
        activity?.runOnUiThread(Runnable {
            when (status) {
                HikVideoPlayerCallback.Status.EXCEPTION -> {
                    stop()
                    viewHolder.setVisible(R.id.t_err, true)
                    viewHolder.setText(
                        R.id.t_err,
                        MessageFormat.format("取流发生异常，错误码：{0}", Integer.toHexString(i))
                    )
                }
                HikVideoPlayerCallback.Status.FAILED -> {
                    viewHolder.setVisible(R.id.t_err, true)
                    viewHolder.setText(
                        R.id.t_err,
                        MessageFormat.format("播放发生异常，错误码：{0}", Integer.toHexString(i))
                    )
                    stop()
                }
                HikVideoPlayerCallback.Status.FINISH -> {
                }
                HikVideoPlayerCallback.Status.SUCCESS -> {
                    viewHolder.setGone(R.id.t_err, false)
                    mPlayerStatus = PlayerStatus.SUCCESS
                }
            }
            viewHolder.setGone(R.id.item_progress, false)
        })

    }

    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {

    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {

    }

    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
        stop()
        return false
    }

    override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
        mPlayer.setSurfaceTexture(surface)
        if (mPlayerStatus !== PlayerStatus.FAILED || mPlayerStatus !== PlayerStatus.EXCEPTION) {
            startPlay()
        }
    }
    private fun stop() {

        object : Thread() {
            override fun run() {
                mPlayer.stopPlay()
            }
        }.start()
    }
    enum class PlayerStatus {
        IDLE, //闲置状态
        LOADING, //加载中状态
        SUCCESS, //播放成功
        STOPPING, //暂时停止播放
        FAILED, //播放失败
        EXCEPTION, //播放过程中出现异常
        FINISH//回放结束
    }
}